<?php
$ws = new Swoole\WebSocket\Server('0.0.0.0',9501);

$ws->on('Open',function ($ws,$request){
    echo  "欢迎,第".$request->fd."号用户进入聊天室";
});

$ws->on('Message',function ($ws,$frame){
     $data = $frame->data;
     $ret['data'] = $data;
     foreach ($ws->connections as $client){
         if ($frame->fd == $client){
             $ret['style'] = "bubble me";
         }else{
             $ret['style'] = "bubble you";
         }
         $ws->push($client,json_encode($ret,256));
     }
});

$ws->on('Close',function ($ws,$fd){
    echo "第{$fd}号用户离开聊天室";
});

$ws->start();

?>